<template>
  <el-dialog
    class="dialog"
    title="修改密码"
    :visible.sync="dialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="handleClose"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
    >
      <el-form-item label="手机号:" prop="phoneNum">
        <el-input maxLength="11" placeholder="请输入手机号" v-model="form.phoneNum"></el-input>
      </el-form-item>

      <el-form-item label="请输入图形验证码:" prop="captcha">
         <div style="position: relative">
           <div >
             <el-input placeholder="请输入图形验证码" v-model="form.captcha"></el-input>
           </div>
           <div>
             <img style="position: absolute;top: 30px;right: 0;height: 50px" @click="changeImg" :src="imgUrl" alt="" />
           </div>
         </div>
      </el-form-item>
      <el-form-item label="请输入验证码:" prop="smsCode">
        <div style="position: relative">
          <div >
            <el-input placeholder="请输入验证码" v-model="form.smsCode"></el-input>
          </div>
          <div style="position: absolute;top: 50%;right: 0">
            <el-button type="text" @click="getECode">{{limitText}}</el-button>
          </div>
        </div>

      </el-form-item>
      <el-form-item label="新密码:" prop="newPassword">
        <el-input show-password type="password" placeholder="请输入新密码" v-model="form.newPassword"></el-input>
      </el-form-item>
      <el-form-item label="再次输入新密码:" prop="againPassword">
        <el-input show-password :disabled="!form.newPassword" placeholder="请重复输入新密码" v-model="form.againPassword"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose()">取 消</el-button>
      <el-button
        size="small"
        :loading="buttonLoading"
        type="primary"
        @click="submit()"
      >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import encryption from '@/utils/encryption'
import { randomImg,getCode, forgetPass} from "@/api/login";
import randomNum from "@/utils/random";
export default {
  name: 'changePassDialog',
  props:{
    dialogVisible:{
      type:Boolean
    }
  },
  data(){

    return {
      rules: {
        captcha:[
          {
            required: true,
            message: "请输入图形验证码",
          }
        ],
        smsCode:[
          {
            required: true,
            message: "请输入验证码",
          }
        ],
        phoneNum:[  {
          required: true,
          message: "请输入手机号",
        },],
        newPassword:[  {
          required: true,
          message: "请输入新密码",
        },],
        againPassword: [
          {
            required: true,
            message: "请再次输入新密码",
          },
          {
            validator: this.validatePhone,
            trigger: "blur",
          },
        ],
      },
      form:{
        phoneNum:"",
        newPassword:"",
        againPassword:"",
        smsCode:"",
        codeType:"1",
        captcha:""
      },
      getForm:{
        loginName:"",
        checkKey:"",
        codeType:"",
      },
      buttonLoading:false,
      imgUrl:"",
      id:"",
      timer:null,
      limitText:"获取验证码"
    }
  },
  mounted() {
    this.changeImg()
  },
  methods:{
    getECode(){
      let is = true
      if(typeof this.limitText === 'number'){
        this.$message.error("请勿频繁获取验证码")
        return
      }
      this.$refs.form.validateField( ["phoneNum","captcha"],(e)=>{
        if(e){
          is = false;
          return false
        }
      })
      if(is){

        getCode({
          loginName: this.form.phoneNum,
          checkKey:this.id,
          codeType:"1",
          captcha: this.form.captcha
        }).then(response=>{
          if(response.code == 200){
            this.limitText = 60;
            this.timer = setInterval(()=>{
              this.limitText = this.limitText -1;
              if(this.limitText == 0){
                clearInterval(this.timer);
                this.limitText = "获取验证码"
              }
            },1000)
            this.$message.success("发送成功")
          }else{
            this.$message.error(response.message)
          }
        })
      }

    },
    changeImg() {
      this.getForm.checkKey = randomNum();
      randomImg(this.getForm.checkKey).then((response) => {
        if (response.code === 0) {
          this.imgUrl = response.result.pic;
          this.id = response.result.id;
        }
      });
    },
    validatePhone (rule, value, callback){
      if (this.form.newPassword != this.form.againPassword) {
        // callback(new Error('长度必须在2-10个之间'))
        callback(new Error("请再次输入的密码与新密码一致"));
      } else {
        callback(); // 通过
      }
    },
    handleClose(){
      this.$emit("close",false);
    },
    submit(){
      this.$refs.form.validate((e)=>{
        if(e){
          this.buttonLoading = true
          forgetPass({
            phoneNum:this.form.phoneNum,
            smsCode:this.form.smsCode,
            codeType:"1",
            newPassword:encryption(this.form.newPassword),
            againPassword:encryption(this.form.againPassword)
          }).then(response=>{
            this.buttonLoading = false
            if(response.code == 200){
              this.$emit("close");
              this.$message.success("修改成功")
            }else{
              this.$message.error(response.message)
            }
          }).catch(()=>{
            this.buttonLoading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-input__inner {
  padding-left: 0px !important;
  border-top-width: 0px;
  border-left-width: 0px;
  border-right-width: 0px;
  border-bottom-width: 1px;
  /*outline: medium;*/
}
::v-deep .el-dialog__body {
  max-height: 55vh;
  overflow-y: auto;
  overflow-x: hidden;
&::-webkit-scrollbar {
   width: 10px;
   border-radius: 16px;
 }

&::-webkit-scrollbar-thumb {
   background: #c1c1c1;
   border-radius: 16px;
 }

&::-webkit-scrollbar-track {
   background: #f1f1f1;
   border-radius: 16px;
 }
}
</style>
